<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends Admin_Controller {
	
	function __construct()
	{
	    parent::__construct();
            $this->load->model('admin_model');
	}
	public function password()
	{	
		if($_POST){
				$mssage = array();
				$old		 = trim($_POST['old']);
				$password	 = trim($_POST['password']);
				$repassword	 = trim($_POST['repassword']);
				
				if(!$old||!$password||!$repassword){
				$mssage['info']	  = '各项密码必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
				}
				
				if($password!=$repassword){
				$mssage['info']	  = '两个输入的新密码不一致';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
				}
				
				$oldpassword = md5($this->userinfo['username'].md5($old));
				$newpassword = md5($this->userinfo['username'].md5($repassword));
				if($oldpassword!=$this->userinfo['password']){
				$mssage['info']	  = '旧密码不正确';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
				}else{
				
				$result = $this->admin_model->update(array('password'=>$newpassword));
				
					if($result){
					$mssage['info']	  = '密码修改成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('member/password');
					echo json_encode($mssage);exit;
					}else{
					$mssage['info']	  = '密码修改不成功';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
					}
				
				
				}
		}
		
		$this->template->load('member/password');
	}
	
	public function safepassword()
	{	
		if($_POST){
				$mssage = array();
				$old		 = trim($_POST['old']);
				$password	 = trim($_POST['password']);
				$repassword	 = trim($_POST['repassword']);
				
				if(!$old||!$password||!$repassword){
				$mssage['info']	  = '各项密码必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
				}
				
				if($password!=$repassword){
				$mssage['info']	  = '两个输入的新密码不一致';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
				}
				
				$oldpassword = md5($this->userinfo['username'].md5($old));
				$newpassword = md5($this->userinfo['username'].md5($repassword));
				if($oldpassword!=$this->userinfo['passwd']){
				$mssage['info']	  = '旧密码不正确';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
				}else{
				
				$result = $this->user_model->update(array('passwd'=>$newpassword));
				
					if($result){
					$mssage['info']	  = '密码修改成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('member/safepassword');
					echo json_encode($mssage);exit;
					}else{
					$mssage['info']	  = '密码修改不成功';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
					}
				
				
				}
				
				
				
				
				
		
		}
		
		$this->template->load('member/safepassword');
	}
	
	public function bankinfo()
	{	
		if($_POST){
				$mssage = array();
				$data = array();
				$password				= trim($_POST['password']);
				$data['bank_name']		= trim($_POST['bank_name']);
				$data['bank_account']	= trim($_POST['bank_account']);
				$data['bank_address']	= trim($_POST['bank_address']);
				
				$safepassword = md5($this->userinfo['username'].md5($password));
				if($safepassword!=$this->userinfo['passwd']){
				$mssage['info']	  = '安全密码不正确';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
				}else{
				
				$result = $this->user_model->update($data);
					if($result){
					$mssage['info']	  = '修改成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('member/bankinfo');
					echo json_encode($mssage);exit;
					}else{
					$mssage['info']	  = '修改不成功';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
					}
				
				
				}
		
		
		
		
		}
		
		$this->template->load('member/bankinfo');
	}
	
	public function register()
	{	
		if($_POST){
				$mssage = array();
				$data = array();
				$username	= trim($_POST['username']);
				$password	= trim($_POST['password']);
				$passwd		= trim($_POST['passwd']);
				$userclass	= trim($_POST['userclass']);
				$referrer	= trim($_POST['referrer']);
				$truename	= trim($_POST['truename']);
				$nickname	= trim($_POST['nickname']);
				$mobile		= trim($_POST['mobile']);
				$qq			= trim($_POST['qq']);
				$email		= trim($_POST['email']);
				$province	= trim($_POST['location_p']);
				$city		= trim($_POST['location_c']);
				$area		= trim($_POST['location_a']);
				$address	= trim($_POST['address']);
				
				$userpass = md5($username.md5($password));
				$safepass = md5($username.md5($passwd));
				
				$data['username']	= $username;
				$data['password']	= $userpass;
				$data['passwd']		= $safepass;
				$data['userclass']	= $userclass;
				$data['referrer']	= $referrer;
				$data['truename']	= $truename;
				$data['nickname']	= $nickname;
				$data['mobile']		= $mobile;
				$data['qq']			= $qq;
				$data['email']		= $email;
				$data['province']	= $province;
				$data['city']		= $city;
				$data['area']		= $area;
				$data['address']	= $address;
				$data['posttime']	= time();
				$data['regip']		= $this->input->ip_address();
				
				
				
				
				if(!$username){
					$mssage['info']	  = '用户名必须输入';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				
				$user = $this->user_model->Get($username);
				if($user){
					$mssage['info']	  = '用户名已经存在';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				
				if(!$password||!$passwd){
					$mssage['info']	  = '密码必须输入';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				if(!$userclass){
					$mssage['info']	  = '会员等级还没有选择';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				if(!$referrer){
					$mssage['info']	  = '推荐人必须填写';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				
				$reuser = $this->user_model->Get($referrer);
				if(!$reuser){
					$mssage['info']	  = '推荐人不存在';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				
				if(!$truename){
					$mssage['info']	  = '用户姓名必须填写';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				if(!$mobile){
					$mssage['info']	  = '手机号码必须填写';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				
					$result = $this->user_model->insert($data);
					if($result){
					$mssage['info']	  = '注册成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('member/teamlist');
					echo json_encode($mssage);exit;
					}else{
					$mssage['info']	  = '注册不成功';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
					}
				
		
		}
		$userclass = $this->user_model->userclass();
		$this->template->assign('userclass',$userclass);
		$this->template->load('member/register');
	}
	
	public function teamlist()
	{	
		$where = array();
		$where['referrer'] = $this->session->userdata('username');
		
		//分页
		$getcount = $this->user_model->get_count($where);
		
		$navigation_position = 'outside';
		$records_per_page = 10;
		$this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
		//$this->xpage->reverse(true);
		$this->xpage->records($getcount);
		$this->xpage->records_per_page($records_per_page);
		$this->xpage->labels("<<", ">>");
		//$this->xpage->labels("上一页", "下一页");
		
		$userlist = $this->user_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page);
		
		foreach($userlist as $key=>$vo){
			$state = '';
			if($vo['state']=='active'){
			$state = '<span class="red">已激活</span>';
			$active = '';
			}else{
			$state = '<span class="red">未激活</span>';
			$active = '<a href="'.site_url('member/active').'?id='.$vo['id'].'">激活</a> ';
			}
			$userlist[$key]['uid']		= $vo['id'];
			$userlist[$key]['username'] = $vo['username'];
			$userlist[$key]['truename'] = $vo['truename'];
			$userlist[$key]['classname'] = $this->user_model->classname($vo['userclass']);;
			$userlist[$key]['state']	 = $state;
			$userlist[$key]['posttime']	 = date('Y-m-d H:i',$vo['posttime']);
			$userlist[$key]['mobile']	 = $vo['mobile'];
			$userlist[$key]['editurl']	 = site_url('member/useredit').'?id='.$vo['id'];
			$userlist[$key]['delurl']	 = site_url('member/userdel').'?id='.$vo['id'];
			$userlist[$key]['opt']		 = $active;
		}

		
		$this->template->assign('Total',$getcount);
		$this->template->assign('showpage',$this->xpage->render(true));
		$this->template->assign('userlist',$userlist);
		$this->template->load('member/teamlist');
	}
	
	public function useredit()
	{	
		if($_POST){
		
		
				$mssage = array();
				$data = array();
				$id	 = intval($_POST['id']);
				
				$password			 = trim($_POST['password']);
				$username			 = trim($_POST['username']);
				if($password){
				$data['password']	 = md5($username.md5($password));
				}
				$data['truename']	 = trim($_POST['truename']);
				$data['nickname']	 = trim($_POST['nickname']);
				$data['mobile']		 = trim($_POST['mobile']);
				$data['qq']			 = trim($_POST['qq']);
				$data['email']		 = trim($_POST['email']);
				$data['province']	 = trim($_POST['location_p']);
				$data['city']		 = trim($_POST['location_c']);
				$data['area']		 = trim($_POST['location_a']);
				$data['address']	 = trim($_POST['address']);
				
				if(!$id){
					$mssage['info']	  = '参数不正确';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}else{
					$result = $this->user_model->userupdate($data,$id);
					if($result){
					$mssage['info']	  = '修改成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('member/index');
					echo json_encode($mssage);exit;
					}else{
					$mssage['info']	  = '修改不成功';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
					}
				}
				
		}
		
		$id = intval($_GET['id']);
		
		if(!$id){
		redirect('member/teamlist');
		}
		
		$user = $this->user_model->Get($id);
		
		//重定义页面
		$segment_p = 'teamlist';
		$this->template->assign('segment_p',$segment_p);
		
		$this->template->assign('user',$user);
		//$this->template->assign('pagetitle','用户管理');
		//$user = $this->user_model->Get($username);
		
		$this->template->load('member/useredit');
	}
	
	public function active()
	{	
		if($_POST){
		
				$mssage = array();
				$password = trim($_POST['password']);
				$uid	  = trim($_POST['uid']);
				if(!$uid){
					$mssage['info']	  = '操作错误，请刷新后请试';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				
				if(!$password){
					$mssage['info']	  = '安全密码不能为空';
					$mssage['status'] = 0;
					$mssage['url']	  = '';
					echo json_encode($mssage);exit;
				}
				
				$safepassword = md5($this->userinfo['username'].md5($password));
				if($safepassword!=$this->userinfo['passwd']){
				$mssage['info']	  = '安全密码不正确';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
				}
				
				$userinfo = $this->user_model->Get($uid);
				$data = array();
				$data['state']		= 'active';
				$data['activetime']	= time();
				$result01 = $this->user_model->userupdate($data,$userinfo['id']);
					
				if($result01){
				$mssage['info']	  = '激活成功';
				$mssage['status'] = 1;
				$mssage['url']	  = site_url('member/teamlist');;
				echo json_encode($mssage);exit;
				}
				
		}
		
		
		$uid = intval($_GET['id']);
		$userinfo = $this->user_model->Get($uid);
		if(!$userinfo){
		redirect('member/teamlist');
		exit;
		}
		
		if($userinfo['referrer']!=$this->userinfo['username']){
		redirect('member/teamlist');
		exit;
		}
	
		//重定义页面
		$segment_p = 'teamlist';
		$this->template->assign('segment_p',$segment_p);
		
		$this->template->assign('uinfo',$this->userinfo);
		$this->template->assign('userinfo',$userinfo);
		
		$this->template->assign('userclass', $this->user_model->classname($userinfo['userclass']));
		
		$this->template->assign('amount',$uclass['amount']);
		$this->template->assign('netpay',$uclass['amount']);
		$this->template->assign('pagetitle','激活会员');
		
		$this->template->load('member/active');
		
	}
	
	
}
